package com.fafa.swing.base;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

/**
 * @author : fafa
 * @version V1.0
 * @Project: SwingTest
 * @Package com.fafa.swing
 * @Description: ToolBarDemo
 * @date Date : 2019年11月25日 22:42
 */
public class ToolBarDemo extends JPanel implements ActionListener {

    protected JTextArea jTextArea;
    protected String newline = "\n";
    static final private String OPEN = "OPEN";
    static final private String SAVE = "SAVE";
    static final private String NEW = "NEW";
    private JButton[] bs = {new JButton("确定"),new JButton("取消"), new JButton("重置")};

    public ToolBarDemo(){
        super(new BorderLayout());
        JToolBar bar = new JToolBar();
        addButtons(bar);
        jTextArea = new JTextArea(15,30);
        jTextArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(jTextArea);

        setPreferredSize(new Dimension(450,110));
        add(bar, BorderLayout.PAGE_START);
        add(scrollPane, BorderLayout.CENTER);
    }

    private void addButtons(JToolBar bar){
        JButton jButton = null;
        jButton = makeNavigationButton("new1",NEW,"新建一个文件","新建");
        bar.add(jButton);
        jButton = makeNavigationButton("open1",OPEN,"打开一个文件","打开");
        bar.add(jButton);
        jButton = makeNavigationButton("save1",SAVE,"保存一个文件","保存");
        bar.add(jButton);

    }
    private JButton makeNavigationButton(String imageName, String actionCommand,
                                      String toolTipText, String altText){
        //搜索图片
        String imgLocation = imageName +".jpg";
        URL imageURL = ToolBarDemo.class.getResource(imgLocation);
        //初始化工具按钮
        JButton button = new JButton();
        button.setActionCommand(actionCommand);
        button.setToolTipText(toolTipText);
        button.addActionListener(this);
        if(imageURL != null){
            button.setIcon(new ImageIcon(imageURL));
        }else{
            button.setText(altText);
            System.err.println("Resource not found:"+imgLocation);
        }
        return button;
    }

    protected  void displayResult(String actionDescription){
        jTextArea.append(actionDescription+newline);
    }

    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame jFrame = new JFrame("工具栏");
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ToolBarDemo newContentPane = new ToolBarDemo();
        newContentPane.setOpaque(true);
        jFrame.setContentPane(newContentPane);
        jFrame.pack();
        jFrame.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().toString().equals(NEW)){
            displayResult("NEW...");
            JOptionPane.showOptionDialog(null,"请选择其中一项：","选择",1,3,null,bs,bs[0]);
        }
        if(e.getActionCommand().toString().equals(SAVE)){
            displayResult("SAVE...");
        }
        if(e.getActionCommand().toString().equals(OPEN)){
            displayResult("OPEN...");
        }
    }
}
